package com.xs.bookspoststation.UserActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.xs.bookspoststation.R;
import com.xs.bookspoststation.bean.User;
import com.xs.bookspoststation.db.DBHelperUser;

public class UpdateUserActivity extends AppCompatActivity implements View.OnClickListener {
    //定义对象
    private EditText edit_update_username2,edit_update_userpwd2,edit_update_userphone2,user_update_education1,
            edit_update_fullname,edit_update_email,edit_update_province,edit_update_school;
    private RadioGroup user_update_radioGroup2;
    private RadioButton user_update_radioButton1,user_update_radioButton2;
    private CheckBox update_user_checkBox1,update_user_checkBox2,update_user_checkBox3;
    private Button btn_update_user_cancel1,btn_update_user1;
    //定义一个数据库帮助类
    private DBHelperUser dbHelperUser;
    //定义一个数据库操作类
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);

        //操作对象的方法
        initView();
    }


    private void initView() {
        //获取用户名控件
        edit_update_username2 = findViewById(R.id.edit_update_username2);
        //获取密码控件
        edit_update_userpwd2 = findViewById(R.id.edit_update_userpwd2);
        //获取电话控件
        edit_update_userphone2 = findViewById(R.id.edit_update_userphone2);
        //获取按钮
        user_update_radioGroup2 = findViewById(R.id.user_update_radioGroup2);
        //获取性别男控件
        user_update_radioButton1 = findViewById(R.id.user_update_radioButton1);
        //获取性别女控件
        user_update_radioButton2 = findViewById(R.id.user_update_radioButton2);
        //获取爱好控件
        update_user_checkBox1 = findViewById(R.id.update_user_checkBox1);
        //获取爱好控件
        update_user_checkBox2 = findViewById(R.id.update_user_checkBox2);
        //获取爱好控件
        update_user_checkBox3 = findViewById(R.id.update_user_checkBox3);
        //获取学历控件
        user_update_education1 = findViewById(R.id.user_update_education1);
        //获取姓名控件
        edit_update_fullname = findViewById(R.id.edit_update_fullname);
        //获取邮箱控件
        edit_update_email = findViewById(R.id.edit_update_email);
        //获取籍贯控件
        edit_update_province = findViewById(R.id.edit_update_province);
        //获取学校控件
        edit_update_school = findViewById(R.id.edit_update_school);
        //获取取消按钮控件
        btn_update_user_cancel1 = findViewById(R.id.btn_update_user_cancel1);
        //获取注册按钮控件
        btn_update_user1 = findViewById(R.id.btn_update_user1);
        //点击事件
        btn_update_user_cancel1.setOnClickListener(this);
        btn_update_user1.setOnClickListener(this);
        //实例化数据库帮助类对象
        dbHelperUser = new DBHelperUser(UpdateUserActivity.this,"books_station.db",null,1);
        //创建数据库并获取数据库的读写权限
        db = dbHelperUser.getWritableDatabase();
    }

    /**
     * 点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_update_user_cancel1:
                Intent intent=new Intent(UpdateUserActivity.this,UserViewActivity.class);
                Toast.makeText(getApplicationContext(), "返回上一界面", Toast.LENGTH_LONG).show();
                startActivity(intent);
                break;
            case R.id.btn_update_user1:
                goUpdate();
                break;
        }
    }

    /**
     * 界面启动执行此方法
     */
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=getIntent();
        //以下是获取查询用户信息界面传过来的数据
        String update_username2 = intent.getStringExtra("name");
        String update_userpwd2 = intent.getStringExtra("pass");
        String update_userphone2 = intent.getStringExtra("userphone");
        String update_education1 = intent.getStringExtra("usereducation");
        String update_fullname = intent.getStringExtra("userfullname");
        String update_email = intent.getStringExtra("useremail");
        String update_province = intent.getStringExtra("userprovince");
        String update_school = intent.getStringExtra("userschool");
        //将这些获取的数据进行显示
        edit_update_username2.setText(update_username2);
        //设置用户名不能修改
        edit_update_username2.setEnabled(false);
        edit_update_userpwd2.setText(update_userpwd2);
        edit_update_userphone2.setText(update_userphone2);
        user_update_education1.setText(update_education1);
        edit_update_fullname.setText(update_fullname);
        edit_update_email.setText(update_email);
        edit_update_province.setText(update_province);
        edit_update_school.setText(update_school);
        //打印日志
        Log.v("操作","！");
    }

    /**
     * 修改方法
     */
    private void goUpdate() {
        //如果点击了更新
        //还是判断一下是否填写完整，
        if(edit_update_username2.length()==0||edit_update_userpwd2.length()==0||edit_update_userphone2.length()==0||
                user_update_education1.length()==0||edit_update_fullname.length()==0||edit_update_email.length()==0||
                edit_update_province.length()==0||edit_update_school.length()==0){
            Toast.makeText(UpdateUserActivity.this, "信息未填写完整！", Toast.LENGTH_LONG).show();
        }else{
            String wantUpdateName = edit_update_username2.getText().toString();
            String wantUpdatePass = edit_update_userpwd2.getText().toString();
            String wantUpdatePhone = edit_update_userphone2.getText().toString();
            String wantUpdateEducation = user_update_education1.getText().toString();
            String wantUpdateFullname = edit_update_fullname.getText().toString();
            String wantUpdateEmail = edit_update_email.getText().toString();
            String wantUpdateProvince = edit_update_province.getText().toString();
            String wantUpdateSchool = edit_update_school.getText().toString();
            User user=new User(wantUpdateName,wantUpdatePass,wantUpdatePhone,null,null,wantUpdateEducation,wantUpdateFullname,wantUpdateEmail,wantUpdateProvince,wantUpdateSchool);
            //数据库操作
            SQLiteDatabase db = dbHelperUser.getWritableDatabase();//
            if(db.isOpen()){
                String name = user.getUsername();
                String pass = user.getUserpwd();
                String userphone = user.getUserphone();
//                String usersex = user.getUsersex();
//                String userhobby = user.getUserhobby();
                String usereducation = user.getUsereducation();
                String userfullname = user.getUserfullname();
                String useremail = user.getUseremail();
                String userprovince = user.getUserprovince();
                String userschool = user.getUserschool();

                //类似于HashMap，但这个只能存储基本类型
                ContentValues c=new ContentValues();
                c.put("username", name);
                c.put("userpwd", pass);
                c.put("userphone", userphone);
                c.put("usereducation", usereducation);
                c.put("userfullname", userfullname);
                c.put("useremail", useremail);
                c.put("userprovince", userprovince);
                c.put("userschool", userschool);
                db.update("book_user", c, "username=?",new String[]{name});
                Log.v("更新用户信息操作", "更新用户信息成功！");
            }else{
                Toast.makeText(UpdateUserActivity.this,"数据库打开失败",Toast.LENGTH_LONG).show();
            }
            Toast.makeText(UpdateUserActivity.this,"用户信息修改成功！",Toast.LENGTH_LONG).show();
            Intent intent2 =new Intent(UpdateUserActivity.this,UserViewActivity.class);
            startActivity(intent2);
        }
    }
}
